package com.csu.ourpetstore1.service;

import com.csu.ourpetstore1.domain.*;
import com.csu.ourpetstore1.persistence.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class OrderService {

  @Autowired
  ItemMapper itemMapper;
  @Autowired
  OrderMapper orderMapper;
  @Autowired
  SequenceMapper sequenceMapper;
  @Autowired
  LineItemMapper lineItemMapper;
  @Autowired
  CartMapper cartMapper;
  
  public void insertOrder(Order order) {
    BigDecimal total= new BigDecimal(0);
    order.setOrderId(getNextId("ordernum"));
    List<CartItem> cart = cartMapper.get_wholeCart(order.getUsername());
    if(!cart.isEmpty()){
      for (CartItem cartItem : cart) {
        order.addLineItem(cartItem);
        cartMapper.removeItemById(order.getUsername(),cartItem.getItem().getItemId());
      }
    }
    for (int i = 0; i < order.getLineItems().size(); i++) {
      LineItem lineItem = order.getLineItems().get(i);
      String itemId = lineItem.getItem().getItemId();
      Integer increment = lineItem.getQuantity();
      Map<String, Object> param = new HashMap<>(2);
      param.put("itemId", itemId);
      param.put("increment", increment);
      itemMapper.updateInventoryQuantity(param);
    }


    orderMapper.insertOrderStatus(order);

    for (int i = 0; i < order.getLineItems().size(); i++) {
      LineItem lineItem = order.getLineItems().get(i);
      lineItem.setOrderId(order.getOrderId());
      lineItemMapper.insertLineItem(lineItem);
      total=total.add(lineItem.getTotal());
    }
    order.setTotalPrice(total);
    orderMapper.insertOrder(order);
  }
  
  public Order getOrder(int orderId) {
    Order order = orderMapper.getOrder(orderId);
    order.setLineItems(lineItemMapper.getLineItemsByOrderId(orderId));

    for (int i = 0; i < order.getLineItems().size(); i++) {
      LineItem lineItem = order.getLineItems().get(i);
      Item item = itemMapper.getItem(lineItem.getItem().getItemId());
      item.setQuantity(itemMapper.getInventoryQuantity(lineItem.getItem().getItemId()));
      lineItem.setItem(item);
    }

    return order;
  }

  public List<Order> getOrders(){
    return orderMapper.getOrders();
  }

  public List<Order> getOrdersByUsername(String username) {
    return orderMapper.getOrdersByUsername(username);
  }

  public int getNextId(String name) {
    Sequence sequence = new Sequence(name, -1);
    sequence = sequenceMapper.getSequence(sequence);
    if (sequence == null) {
      throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name
          + " sequence).");
    }
    Sequence parameterObject = new Sequence(name, sequence.getNextId() + 1);
    sequenceMapper.updateSequence(parameterObject);
    return sequence.getNextId();
  }

  public void updateOrder(int orderId){
    orderMapper.updateOrder(orderId);
  }

  public void deleteOrder(int orderId){
    orderMapper.deleteOrder(orderId);
    orderMapper.deleteOrderStatus(orderId);
    lineItemMapper.deleteLineItem(orderId);
  }

}
